/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sqlserver;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
/**
 *
 * @author ROMANCE
 */

public class GetElementXml{
    public String[] GetAllElementFromXML(String xmlFile){
        try{
                String[] nodeName = null;
                File file = new File(xmlFile);
                if(file.exists()){
                        // Create a factory
                        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                        // Use the factory to create a builder
                        DocumentBuilder builder = factory.newDocumentBuilder();
                        Document doc = builder.parse(xmlFile);
                        // Get a list of all elements in the document
                        NodeList list = doc.getElementsByTagName("*");
                        for (int i=0; i<list.getLength(); i++) {
                                // Get element
                                Element element = (Element)list.item(i);                                   
                                nodeName[i] = element.getNodeName();
                        }
                        return nodeName;
                }
                else{
                        System.out.print("File not found!");
                        return null;
                }
        }
        catch(IOException | ParserConfigurationException | SAXException e) {
            return null;
        }
    }
    
    public String[] GetOneElementFromXML(String xmlFile, String nodeName){
        try{
                String[] result = null;
                File file = new File(xmlFile);
                if(file.exists()){
                        // Create a factory
                        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                        // Use the factory to create a builder
                        DocumentBuilder builder = factory.newDocumentBuilder();
                        Document doc = builder.parse(xmlFile);
                        // Get a list of all elements in the document
                        NodeList list = doc.getElementsByTagName(nodeName);
                        for (int i=0; i<list.getLength(); i++) {
                                // Get element
                                Element element = (Element)list.item(i);                                   
                                System.out.println(element.getNodeName());
                                result[i] = element.getNodeName();
                        }
                        return result;
                }
                else{
                        System.out.print("File not found!");
                        return null;
                }
        }
        catch(IOException | ParserConfigurationException | SAXException e) {
		   System.exit(1);
                   return null;
        }
    }
    
    public int CountXmlElementByName(String xmlFile, String elementName){
        try {
                int count = 0;
                File file = new File(xmlFile);
                if (file.exists()){
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                // Create the builder and parse the file
                Document doc = factory.newDocumentBuilder().parse(xmlFile);
                NodeList nodes = doc.getElementsByTagName(elementName);
                count = nodes.getLength();
                return count;
        }
        else{
            System.out.print("File not found!");
            return 0;
        }
                
        }
        catch (ParserConfigurationException | SAXException | IOException ex) {
            System.out.println(ex);
            return 0;
        }

    }
    
    public void GetAllDataFromXML(String xmlFile){
        try{
                File file = new File(xmlFile);
                if (file.exists()){
                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder builder = factory.newDocumentBuilder();
                    Document doc = builder.parse(xmlFile);
                    //  Create transformer
                    Transformer tFormer = TransformerFactory.newInstance().newTransformer();
                    //  Output text type
                    tFormer.setOutputProperty(OutputKeys.METHOD, "text");
                    //  Write the document to a file
                    Source source = new DOMSource(doc);
                    Result result = new StreamResult(System.out);
                    tFormer.transform(source, result);
            }
            else{
                    System.out.println("File not found!");
            }
        }
        catch (ParserConfigurationException | SAXException | IOException | TransformerFactoryConfigurationError | IllegalArgumentException | TransformerException e){
            System.err.println(e);
            System.exit(0);
        }  
    }
    
    public void AccessingXmlFileByRootName(String xmlFile, String rootName){
        try {
            File file = new File(xmlFile);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(file);
            document.getDocumentElement().normalize();
            System.out.println("Root element " + document.getDocumentElement().getNodeName());
            NodeList node = document.getElementsByTagName(rootName);
            
            for (int i = 0; i < node.getLength(); i++) {
                Node firstNode = node.item(i);

                if (firstNode.getNodeType() == Node.ELEMENT_NODE) {

                Element element = (Element) firstNode;
                
                NodeList firstNameElemntList = element.getElementsByTagName("firstname");
                Element firstNameElement = (Element) firstNameElemntList.item(0);
                NodeList firstName = firstNameElement.getChildNodes();                
                System.out.println("First Name:" + ((Node)firstName.item(0)).getNodeValue());

                NodeList lastNameElementList = element.getElementsByTagName("lastname");
                Element lastNameElement = (Element) lastNameElementList.item(0);
                NodeList lastName = lastNameElement.getChildNodes();                
                System.out.println("Last Name : " + ((Node) lastName.item(0)).getNodeValue());

                NodeList addressList = element.getElementsByTagName("address");
                Element addressElement = (Element) addressList.item(0);
                NodeList address = addressElement.getChildNodes();
                System.out.println("Address : "  + ((Node) address.item(0)).getNodeValue());

                NodeList cityList = element.getElementsByTagName("city");
                Element cityElement = (Element) cityList.item(0);
                NodeList city = cityElement.getChildNodes();
                System.out.println("City : "  + ((Node) city.item(0)).getNodeValue());
                }
            }
            } catch (ParserConfigurationException | SAXException | IOException | DOMException e) {
        }
    }
}